home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / GDEVPCL.C < prev    next >
C/C++ Source or Header  |  1992-03-05  |  7KB  |  203 lines

  1. /* Copyright (C) 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gdevpcl.c */
  21. /* Utilities for PCL printers */
  22. #include "gdevprn.h"
  23. #include "gdevpcl.h"
  24.  
  25. /* ------ Color mapping ------ */
  26.  
  27. /* The PaintJet and DeskJet 500C use additive colors in separate planes. */
  28. /* We only keep one bit of color, with 1 = R, 2 = G, 4 = B. */
  29. /* Because the buffering routines assume 0 = white, */
  30. /* we complement all the color components. */
  31. #define cv_shift (sizeof(gx_color_value) * 8 - 1)
  32.  
  33. /* Map an RGB color to a printer color. */
  34. gx_color_index
  35. gdev_pcl_3bit_map_rgb_color(gx_device *dev,
  36.   gx_color_value r, gx_color_value g, gx_color_value b)
  37. {    return (((b >> cv_shift) << 2) + ((g >> cv_shift) << 1) + (r >> cv_shift)) ^ 7;
  38. }
  39.  
  40. /* Map the printer color back to RGB. */
  41. int
  42. gdev_pcl_3bit_map_color_rgb(gx_device *dev, gx_color_index color,
  43.   gx_color_value prgb[3])
  44. {    ushort cc = (ushort)color ^ 7;
  45.     prgb[0] = -(cc & 1);
  46.     prgb[1] = -((cc >> 1) & 1);
  47.     prgb[2] = -(cc >> 2);
  48.     return 0;
  49. }
  50.  
  51. /* ------ Compression ------ */
  52.  
  53. /*
  54.  * Mode 2 Row compression routine for the HP DeskJet & LaserJet IIp.
  55.  * Compresses data from row up to end_row, storing the result
  56.  * starting at compressed.  Returns the number of bytes stored.
  57.  * Runs of K<=127 literal bytes are encoded as K-1 followed by
  58.  * the bytes; runs of 2<=K<=127 identical bytes are encoded as
  59.  * 257-K followed by the byte.
  60.  * In the worst case, the result is N+(N/127)+1 bytes long,
  61.  * where N is the original byte count (end_row - row).
  62.  * To speed up the search, we examine an entire word at a time.
  63.  * We will miss a few blocks of identical bytes; tant pis.
  64.  */
  65. int
  66. gdev_pcl_mode2compress(const word *row, const word *end_row, char *compressed)
  67. {    register const word *exam = row; /* word being examined in the row to compress */
  68.     register char *cptr = compressed; /* output pointer into compressed bytes */
  69.  
  70.     while ( exam < end_row )
  71.        {    /* Search ahead in the input looking for a run */
  72.         /* of at least 4 identical bytes. */
  73.         const char *compr = (const char *)exam;
  74.         const char *end_dis;
  75.         const word *next;
  76.         register word test;
  77.         while ( exam < end_row )
  78.           { test = *exam;
  79.             if ( ((test << 8) ^ test) <= 0xff )
  80.               break;
  81.             exam++;
  82.           }
  83.  
  84.         /* Find out how long the run is */
  85.         end_dis = (const char *)exam;
  86.         if ( exam == end_row )    /* no run */
  87.           { /* See if any of the last 3 "dissimilar" bytes are 0. */
  88.             if ( end_dis > compr && end_dis[-1] == 0 )
  89.               { if ( end_dis[-2] != 0 ) end_dis--;
  90.             else if ( end_dis[-3] != 0 ) end_dis -= 2;
  91.             else end_dis -= 3;
  92.               }
  93.             next = --end_row;
  94.           }
  95.         else
  96.           { next = exam + 1;
  97.             while ( next < end_row && *next == test )
  98.               next++;
  99.             /* See if any of the last 3 "dissimilar" bytes */
  100.             /* are the same as the repeated byte. */
  101.             if ( end_dis > compr && end_dis[-1] == (byte)test )
  102.               { if ( end_dis[-2] != (byte)test ) end_dis--;
  103.             else if ( end_dis[-3] != (byte)test ) end_dis -= 2;
  104.             else end_dis -= 3;
  105.               }
  106.           }
  107.  
  108.         /* Now [compr..end_dis) should be encoded as dissimilar, */
  109.         /* and [end_dis..next) should be encoded as similar. */
  110.         /* Note that either of these ranges may be empty. */
  111.  
  112.         for ( ; ; )
  113.            {    /* Encode up to 127 dissimilar bytes */
  114.             uint count = end_dis - compr; /* uint for faster switch */
  115.             switch ( count )
  116.               { /* Use memcpy only if it's worthwhile. */
  117.               case 6: cptr[6] = compr[5];
  118.               case 5: cptr[5] = compr[4];
  119.               case 4: cptr[4] = compr[3];
  120.               case 3: cptr[3] = compr[2];
  121.               case 2: cptr[2] = compr[1];
  122.               case 1: cptr[1] = compr[0];
  123.                 *cptr = count - 1;
  124.                 cptr += count + 1;
  125.               case 0: /* all done */
  126.                 break;
  127.               default:
  128.                 if ( count > 127 ) count = 127;
  129.                 *cptr++ = count - 1;
  130.                 memcpy(cptr, compr, count);
  131.                 cptr += count, compr += count;
  132.                 continue;
  133.               }
  134.             break;
  135.            }
  136.  
  137.            {    /* Encode up to 127 similar bytes. */
  138.             /* Note that count may be <0 at end of row. */
  139.             int count = (const char *)next - end_dis;
  140.             while ( count > 0 )
  141.               { int this = (count > 127 ? 127 : count);
  142.                 *cptr++ = 257 - this;
  143.                 *cptr++ = (byte)test;
  144.                 count -= this;
  145.               }
  146.             exam = next;
  147.            }
  148.        }
  149.     return (cptr - compressed);
  150. }
  151.  
  152. /*
  153.  * Mode 3 compression routine for the HP LaserJet III family.
  154.  * Compresses bytecount bytes starting at current, storing the result
  155.  * in compressed, comparing against and updating previous.
  156.  * Returns the number of bytes stored.  In the worst case,
  157.  * the number of bytes is bytecount+(bytecount/8)+1.
  158.  */
  159. int
  160. gdev_pcl_mode3compress(int bytecount, const char *current, char *previous, char *compressed)
  161. {    register const char *cur = current;
  162.     register char *prev = previous;
  163.     register char *out = compressed;
  164.     const char *end = current + bytecount;
  165.     while ( cur < end )
  166.        {    /* Detect a maximum run of unchanged bytes. */
  167.         const char *run = cur;
  168.         register const char *diff;
  169.         const char *stop;
  170.         int offset, cbyte;
  171.         while ( cur < end && *cur == *prev )
  172.            {    cur++, prev++;
  173.            }
  174.         if ( cur == end ) break;    /* rest of row is unchanged */
  175.         /* Detect a run of up to 8 changed bytes. */
  176.         /* We know that *cur != *prev. */
  177.         diff = cur;
  178.         stop = (end - cur > 8 ? cur + 8 : end);
  179.         do
  180.            {    *prev++ = *cur++;
  181.            }
  182.         while ( cur < stop && *cur != *prev );
  183.         /* Now [run..diff) are unchanged, and */
  184.         /* [diff..cur) are changed. */
  185.         /* Generate the command byte(s). */
  186.         offset = diff - run;
  187.         cbyte = (cur - diff - 1) << 5;
  188.         if ( offset < 31 )
  189.             *out++ = cbyte + offset;
  190.         else
  191.            {    *out++ = cbyte + 31;
  192.             offset -= 31;
  193.             while ( offset >= 255 )
  194.                 *out++ = 255, offset -= 255;
  195.             *out++ = offset;
  196.            }
  197.         /* Copy the changed data. */
  198.         while ( diff < cur )
  199.             *out++ = *diff++;
  200.        }
  201.     return out - compressed;
  202. }
  203.